function LinkedList() {
  function Node(data) {
    this.data = data
    this.next = null
  }
  this.head = null
  this.length = 0
  LinkedList.prototype.append = (data) => {
    let newNode = new Node(data)
    if (this.length == 0) {
      this.head = newNode
    } else {
      let current = this.head
      while (current.next) {
        current = current.next
      }
      current.next = newNode
    }
    this.length += 1
  }
  LinkedList.prototype.toString = () => {
    let current = this.head
    let listString = ''
    while (current) {
      listString += ',' + current.data
      current = current.next
    }
    return listString.slice(1)
  }
  LinkedList.prototype.insert = (position, data) => {
    if (position < 0 || position > this.length) {
      return false
    }
    let newNode = new Node(data)
    if (position == 0) {
      newNode.next = this.head
      this.head = newNode
    } else {
      let index = 0
      let current = this.head
      let previous = null
      while (index++ < position) {
        previous = current
        current = current.next
      }
      newNode.next = current
      previous.next = newNode
    }
  }
  LinkedList.prototype.get = (position) => {
    if (position < 0 || position >= this.length) {
      return null
    }
    let current = this.head
    let index = 0
    while (index++ < position) {
      current = current.next
    }
    return current.data
  }
  LinkedList.prototype.indexOf = (data) => {
    let current = this.head
    let index = 0
    while (current) {
      if (current.data == data) {
        return index
      }
      current = current.next
      index += 1
    }
    return -1
  }
  LinkedList.prototype.remove = (data) => {
    let position = this.indexOf(data)
    return this.removeAt(position)
  }
  LinkedList.prototype.removeAt = (posiiton) => {
    if (position < 0 || position >= this.length) {
      return null
    }
    let current = this.head
    if (position == 0) {
      this.head = this.head.next
    } else {
      let index = 0
      let previous = null
      while (index++ < position) {
        previous = current
        current = current.next
      }
      previous.next = current.next
    }
    this.length -= 1
    return current.data
  }
}